﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using XREditor.Dtos;
using XREditor.Models;

namespace XREditor.Controllers
{
    /// <summary>
    /// 模型管理
    /// </summary>
    [DynamicApiController]
    public class MoXingController
    {
        private readonly ILogger<MoXingController> logger;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public MoXingController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
            this.logger = logger;
        }

        /// <summary>
        /// 模型列表
        /// </summary>
        /// <param name="ulsfd">参照下方Schemas中：MoXingSearchListFormDto</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> List([FromBody] MoXingSearchListFormDto mxslfd)
        {
            RefAsync<int> total = 0;
            List<MoXing> moxings = await db.Queryable<MoXing>()
                .Where(mx => mx.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(mxslfd.Content), mx => mx.Title.Contains(mxslfd.Content))
                .WhereIF(!string.IsNullOrEmpty(mxslfd.Type), mx => mx.Type == mxslfd.Type)
                .ToPageListAsync(mxslfd.PageIndex, mxslfd.PageSize, total);

            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = moxings.Adapt<List<MoXingInfoFormDto>>(),
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加模型
        /// </summary>
        /// <param name="uifd">参照下方Schemas中：MoXingInfoFormDto</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Save([FromBody] MoXingInfoFormDto mxifd)
        {
            await db.Insertable<MoXing>(mxifd.Adapt<MoXing>()).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = mxifd,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除模型
        /// </summary>
        /// <param name="pfd">参照下方Schemas中：PrimaryFormDto</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete([FromBody] PrimaryFormDto pfd) {
            await db.Updateable<MoXing>().SetColumns(mx => mx.IsActive == 0).Where(mx => mx.Id == pfd.Id).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 修改模型
        /// </summary>
        /// <param name="uifd">参照下方Schemas中：MoXingInfoFormDto</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] MoXingInfoFormDto mxifd) {
            await db.Updateable<MoXing>(mxifd.Adapt<MoXing>()).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = mxifd,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看模型
        /// </summary>
        /// <param name="pfd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show([FromBody] PrimaryFormDto pfd) {
            MoXing moxing = await db.Queryable<MoXing>()
                .InSingleAsync(pfd.Id);
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = moxing.Adapt<MoXingInfoFormDto>(),
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
